<?php
namespace app\api\service;

use app\api\service\ProductSpecsName as ServiceProductSpecsName;
use app\model\Product as ProductModel;
use app\model\ProductSpecsName;
use app\model\ProductSpecsValue;

class Product
{
    public function getPageData($page,$size,$where)
    {
        // return ProductModel::with('specs')->where($where)->order('id','desc')->paginate($size,false,[
        //     'page'=>$page
        // ]);
         return ProductModel::where($where)->order('id','asc')->paginate($size,false,[
             'page'=>$page
         ]);
    }
    public  function readByID($id)
    {
        $res= ProductModel::where('id',$id)->find();
        $res->specs;
        $specsname=ProductSpecsName::select();
        $specsvalue=ProductSpecsValue::select();
        $res->specsname=$specsname;
        $res->specsvalue=$specsvalue;
        
        $namids=array();
        foreach($res->specshas as $ids)
        {
            $nameid= $ids['specs_value_id'][0];
            array_push($namids,$nameid);
        }
       $newarry=array_unique($namids);
       $specename=(new ServiceProductSpecsName())->getlist($newarry);
        $res->specename=$specename;
        return $res;
    }
    public function deleteByID($id)
    {
        return  ProductModel::destroy($id);
    }
    public function hasEmpty($id)
    {
        $result=ProductModel::findOrEmpty($id)->isEmpty();
        return  $result;
    }
}
